package com.xframe.domain.sys;

import com.xframe.domain.BaseModel;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.xframe.xdal.core.DataBaseAccess;
import com.xframe.xdal.core.annotation.*;
import com.xframe.xdal.core.autofill.specification.ICacheKeyGenerate;
import com.xframe.xdal.core.constant.AutoFillType;
import com.xframe.xdal.core.domain.SysBaseEntity;
import com.xframe.xdal.core.model.ConditionValue;
import com.xframe.xdal.core.model.DbSort;
import com.xframe.xdal.core.model.FindOptions;
import com.xframe.xdal.core.util.IdCreateUtil;
import com.xframe.xdal.core.util.StringUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

import static com.xframe.xdal.core.constant.AutoFillType.ROLEVAL;

/**
 * 模型行为表
 * com.xframe.domain.sys.SysModelAct
 * @author xufeng
 * @version 1.0
 * @date 2023-03-28
 */
@Data
@XTable(value = "SYS_MODEL_ACT", label = "模型动作")
@XSelectItem(keyName = "modelActId",codeName = "code",textName = "name")
public class SysModelAct extends BaseModel implements ICacheKeyGenerate {
    /**
     * 模型行为ID
     */
    @AutoFill(AutoFillType.Snowflake)
    @JsonSerialize(using = com.fasterxml.jackson.databind.ser.std.ToStringSerializer.class)
    @XColumn(name = "MODEL_ACT_ID", primaryKeyFlag = true,desc = "模型动作ID")
    private Long modelActId;

    /**
     * 模型ID
     */
    @JsonSerialize(using = com.fasterxml.jackson.databind.ser.std.ToStringSerializer.class)
    @XColumn(name = "MODEL_ID", uniqueFlag = true,desc = "模型ID")
    private Long modelId;
    /**
     * 模型编码
     */
    @XColumn(name = "MODEL_CODE",desc = "模型编码",length = 50)
    private String modelCode;

    /**
     * 操作结束后是否需要提示 0 = 否 1 = 是
     */
    @XColumn(name = "PROMPT_FLAG",desc = "操作结束后是否需要提示")
    private Integer promptFlag = 1;

    /**
     * 操作结束后提示信息
     */
    @XColumn(name = "PROMPT_MSG",desc = "操作结束后提示信息",nullFlag = true)
    private String promptMsg = "操作成功";

    /**
     * 是否需要刷新页面 0 = 否 1 = 是
     */
    @XColumn(name = "REFRESH_FLAG",desc = "是否需要刷新页面")
    private Integer refreshFlag = 0;

    /**
     * 行为编码
     */
    @XQueryCondition
    @XGridTableRef
    @XColumn(name = "CODE", uniqueFlag = true,length = 20,desc = "行为编码")
    private String code;

    /**
     * 名称
     */
    @XQueryCondition
    @XGridTableRef
    @XColumn(name = "NAME",length = 20,desc = "名称")
    private String name;

    /**
     * 命令编码
     */
    @XQueryCondition
    @XColumn(name = "COMMAND_CODE",length = 50,desc = "命令编码")
    private String commandCode;


    /**
     * 命令参数
     */
    @XColumn(name = "COMMANDARGS",length = 50,desc = "命令参数",nullFlag = true)
    private String commandArgs;

    /**
     * 是否需要选择 0 = 否 1 = 是
     */
    @XColumn(name = "SELECT_FLAG",desc = "是否需要选择")
    private Integer selectFlag = 0;

    /**
     * 是否支持右键菜单 0 = 否 1 = 是
     */
    @XColumn(name = "POP_FLAG",desc = "是否支持右键菜单")
    private Integer popFlag = 0;

    /**
     * 是否多选 0 = 否 1 = 是
     */
    @XColumn(name = "M_SELECT_FLAG",desc = "是否多选")
    private Integer multipleSelectFlag = 0;

    /**
     * 参数列表
     */
    @XColumn(name = "PARAMETER_STRING",desc = "参数列表",length = 1000,nullFlag = true)
    private String parameter;

    /**
     * 查询KEY
     */
    @XColumn(name = "QUERY_KEY",desc = "查询KEY",length = 50,nullFlag = true)
    private String queryKey = "default";

    /**
     * 配置文件版本
     */
    @XColumn(name = "VER_NO",desc = "配置文件版本",length = 20,nullFlag = true)
    private String verNo = "v1";

    /**
     * 转页面路径
     */
    @XColumn(name = "ACTION_PAGE",desc = "转页面路径",length = 500,nullFlag = true)
    private String actionPage;


    /**
     * 按钮处理类别
     */
    @XColumn(name = "ACTION_TYPE",desc = "按钮处理类别",length = 20,nullFlag = true)
    private String actionType;


    /**
     * 排序
     */
    @XColumn(name = "ACTION_ORDER",desc = "排序")
    private Integer actionOrder = 0;

    /**
     * 是否纯脚本 0 = 否 1 = 是
     */
    @XColumn(name = "SCRIPT_FLAG",desc = "是否纯脚本")
    private Integer scriptFlag = 0;

    /**
     * 脚本函数名
     */
    @XColumn(name = "SCRIPT_FUN",desc = "脚本函数名",length = 200,nullFlag = true)
    private String scriptFun;

    /**
     * 是否需要弹出框 0 = 否 1 = 是
     */
    @XColumn(name = "CONFIRM_FLAG",desc = "是否需要弹出框")
    private Integer confirmFlag = 0;

    /**
     * 弹出对话框的内容
     */
    @XColumn(name = "CONFIRM_MSG",desc = "弹出对话框的内容",length = 200,nullFlag = true)
    private String confirmMsg;

    /**
     * 弹出框文本标题
     */
    @XColumn(name = "TITLE_TEXT",desc = "弹出框文本标题",length = 50,nullFlag = true)
    private String titleText;

    /**
     * 视图类型
     */
    @XColumn(name = "CONFIG_TYPE",desc = "视图类型",length = 50,nullFlag = true)
    private String configType;
    /**
     * 客户端表达式(右键菜单可用状态)
     */
    @XColumn(name = "CILENT_EXP",desc = "客户端表达式",length = 1000,nullFlag = true)
    private String exp;
    /**
     * 图标
     */
    @XColumn(name = "icon",desc = "图标",length = 50,nullFlag = true)
    private String icon;
    /**
     * 确认对话框的类型
     */
    @XColumn(name = "confirm_type",desc = "对话框类型",length = 50,nullFlag = true)
    private String confirmType;

    /**
     * 业务组件
     */
    @XColumn(name = "render_class",desc = "业务组件",length = 200,nullFlag = true)
    private String renderClass;

    /**
     * 跳转模型编码
     */
    @XColumn(name = "tar_model_code",desc = "跳转模型编码",length = 200,nullFlag = true)
    private String tarModelCode;

    /**
     * 业务组件配置JSON
     */
    @XColumn(name = "ch_com_config",desc = "跳转模型编码",length = 2000,nullFlag = true)
    private String chComConfig;

    @Override
    public String getCacheKey() {
        return this.modelId + "-ACT";
    }


    private String configId;
    /**
     * 根据注解对象来创建一个SysModelAct对象
     */
    public static SysModelAct createAction(SysModel sysModel,XAction xAction,int orderNo){
        SysModelAct action = new SysModelAct();
        action.setModelActId(IdCreateUtil.getSnowflakeId());
        action.setModelId(sysModel.getModelId());
        action.setModelCode(sysModel.getModelCode());
        action.setActionPage(xAction.actionPage());
        action.setActionOrder(orderNo);
        action.setActionType(xAction.actionType());
        action.setCommandCode(xAction.command());
        action.setCommandArgs(xAction.code());
        action.setCode(xAction.code());
        action.setName(xAction.i18Code());
        action.setConfirmFlag(xAction.confirmFlag());
        action.setConfirmMsg(xAction.confirmMsg());
        action.setSelectFlag(1);
        action.setMultipleSelectFlag(0);
        action.setPromptFlag(1);
        action.setPromptMsg("message.success");  // 操作成功
        action.setRefreshFlag(1);
        action.setPopFlag(1);
        action.setConfigType(StringUtil.EMPTY);
        action.setParameter(xAction.parameter());
        action.setTitleText(xAction.desc());
        action.setCreateBy(sysModel.getCreateBy());
        action.setUpdateBy(sysModel.getCreateBy());
        return action;
    }

    public static List<SysModelAct> readSysModelActByModelId(Long modelId){

        FindOptions findOptions = FindOptions.defFindAllOptions();
        findOptions.getRefList().add(SysBaseEntity.FULL_DATA);
        findOptions.getConditionValueCollection().add(new ConditionValue("modelId",modelId));
        findOptions.getConditionValueCollection().add(new ConditionValue(DELFLAG_NAME,DEL_FALG_N));
        findOptions.getDbSortCollection().add(new DbSort("actionOrder", DbSort.SortDir.ASC));
        List<SysModelAct> sysModelActs = DataBaseAccess.getInstance().findAll(SysModelAct.class,findOptions);
        return sysModelActs;
    }




    public static List<SysModelAct> getDefActionList(SysModel sysModel){
        List<SysModelAct> actionList = new ArrayList<>();
        // 新增按钮
        SysModelAct addAction = new SysModelAct();
        addAction.setModelActId(IdCreateUtil.getSnowflakeId());
        addAction.setModelId(sysModel.getModelId());
        addAction.setModelCode(sysModel.getModelCode());
        addAction.setActionPage("showAddModal");
        addAction.setActionOrder(10);
        addAction.setActionType("2");
        addAction.setCommandArgs(StringUtil.EMPTY);
        addAction.setCommandCode("SaveNew");
        addAction.setCode("add");
        addAction.setName("button.add"); // 新增
        addAction.setConfirmFlag(0);
        addAction.setConfirmMsg(StringUtil.EMPTY);
        addAction.setSelectFlag(0);
        addAction.setMultipleSelectFlag(0);
        addAction.setPromptFlag(1);
        addAction.setPromptMsg("message.success");
        addAction.setRefreshFlag(1);
        addAction.setPopFlag(0);
        addAction.setParameter(StringUtil.EMPTY);
        addAction.setConfigType("add");
        addAction.setIcon("add");
        addAction.setTitleText("新增" + sysModel.getModelName());
        addAction.setCreateBy(sysModel.getCreateBy());
        addAction.setUpdateBy(sysModel.getCreateBy());
        actionList.add(addAction);

        SysModelAct updateAction = new SysModelAct();
        updateAction.setModelActId(IdCreateUtil.getSnowflakeId());
        updateAction.setModelId(sysModel.getModelId());
        updateAction.setModelCode(sysModel.getModelCode());
        updateAction.setActionPage("showAddModal");
        updateAction.setActionOrder(20);
        updateAction.setActionType("2");
        updateAction.setCommandArgs(StringUtil.EMPTY);
        updateAction.setCommandCode("Update");
        updateAction.setCode("edit");
        updateAction.setName("button.edit");
        updateAction.setConfirmFlag(0);
        updateAction.setConfirmMsg("");
        updateAction.setSelectFlag(1);
        updateAction.setMultipleSelectFlag(0);
        updateAction.setPromptFlag(1);
        updateAction.setPromptMsg("message.success");
        updateAction.setRefreshFlag(1);
        updateAction.setPopFlag(1);
        updateAction.setConfigType("edit");
        updateAction.setIcon("edit");
        updateAction.setParameter(StringUtil.EMPTY);
        updateAction.setTitleText("修改" + sysModel.getModelName());
        updateAction.setCreateBy(sysModel.getCreateBy());
        updateAction.setUpdateBy(sysModel.getCreateBy());
        actionList.add(updateAction);


        SysModelAct delAction = new SysModelAct();
        delAction.setModelActId(IdCreateUtil.getSnowflakeId());
        delAction.setModelId(sysModel.getModelId());
        delAction.setModelCode(sysModel.getModelCode());
        delAction.setActionPage("NONE");
        delAction.setActionOrder(30);
        delAction.setActionType("3");
        delAction.setCommandArgs(StringUtil.EMPTY);
        delAction.setCommandCode("Delete");
        delAction.setCode("delete");
        delAction.setName("button.delete");
        delAction.setConfirmFlag(1);
        delAction.setConfirmMsg("message.confirmDelete");
        delAction.setSelectFlag(1);
        delAction.setMultipleSelectFlag(1);
        delAction.setPromptFlag(1);
        delAction.setPromptMsg("message.success");
        delAction.setRefreshFlag(1);
        delAction.setPopFlag(1);
        delAction.setConfigType(StringUtil.EMPTY);
        delAction.setParameter(StringUtil.EMPTY);
        delAction.setTitleText("删除" + sysModel.getModelName());
        delAction.setIcon("delete");
        delAction.setConfirmType("warning");
        delAction.setCreateBy(sysModel.getCreateBy());
        delAction.setUpdateBy(sysModel.getCreateBy());
        actionList.add(delAction);

        SysModelAct logicalDeletAction = new SysModelAct();
        logicalDeletAction.setModelActId(IdCreateUtil.getSnowflakeId());
        logicalDeletAction.setModelId(sysModel.getModelId());
        logicalDeletAction.setModelCode(sysModel.getModelCode());
        logicalDeletAction.setActionPage("NONE");
        logicalDeletAction.setActionOrder(40);
        logicalDeletAction.setActionType("3");
        logicalDeletAction.setCommandArgs(StringUtil.EMPTY);
        logicalDeletAction.setCommandCode("LogicalDelet");
        logicalDeletAction.setCode("logicalDelet");
        logicalDeletAction.setName("button.logicalDelet");
        logicalDeletAction.setConfirmFlag(1);
        logicalDeletAction.setConfirmMsg("message.confirmlogicalDelet");
        logicalDeletAction.setSelectFlag(1);
        logicalDeletAction.setMultipleSelectFlag(1);
        logicalDeletAction.setPromptFlag(1);
        logicalDeletAction.setPromptMsg("message.success");
        logicalDeletAction.setRefreshFlag(1);
        logicalDeletAction.setPopFlag(1);
        logicalDeletAction.setConfigType(StringUtil.EMPTY);
        logicalDeletAction.setParameter(StringUtil.EMPTY);
        logicalDeletAction.setTitleText("停用" + sysModel.getModelName());
        logicalDeletAction.setIcon("close");
        logicalDeletAction.setConfirmType("warning");
        logicalDeletAction.setCreateBy(sysModel.getCreateBy());
        logicalDeletAction.setUpdateBy(sysModel.getCreateBy());
        actionList.add(logicalDeletAction);

        SysModelAct viewAction = new SysModelAct();
        viewAction.setModelActId(IdCreateUtil.getSnowflakeId());
        viewAction.setModelId(sysModel.getModelId());
        viewAction.setModelCode(sysModel.getModelCode());
        viewAction.setActionPage("showDetailModal");
        viewAction.setActionOrder(50);
        viewAction.setActionType("2");
        viewAction.setCommandArgs(StringUtil.EMPTY);
        viewAction.setCommandCode("NONE");
        viewAction.setCode("viewData");
        viewAction.setName("button.detail");
        viewAction.setConfirmFlag(0);
        viewAction.setConfirmMsg(StringUtil.EMPTY);
        viewAction.setSelectFlag(1);
        viewAction.setMultipleSelectFlag(0);
        viewAction.setPromptFlag(0);
        viewAction.setPromptMsg(StringUtil.EMPTY);
        viewAction.setRefreshFlag(0);
        viewAction.setPopFlag(1);
        viewAction.setConfigType("detail");
        viewAction.setIcon("view");
        viewAction.setParameter(StringUtil.EMPTY);
        viewAction.setTitleText("查看" + sysModel.getModelName());
        viewAction.setCreateBy(sysModel.getCreateBy());
        viewAction.setUpdateBy(sysModel.getCreateBy());
        viewAction.setQueryKey("full");
        viewAction.setVerNo("v1");
        actionList.add(viewAction);
        return actionList;

    }
}
